Static Statement
Creates a local variable or local array with the name and size (in the case of an array) and data type specified. A variable declared with the Static statement and assigned a value retains its value from one invocation of the method to the next.
In contrast, variables declared with the Dim statement are completely local to the method and are destroyed when each invocation of the method goes out of scope.
Syntax
The Static statement has two syntaxes:
Static variableName [,variableNameN] As [ New] dataType
Part | Type | Description |
variableName | Variable name | The name of the new variable. |
variableNameN | Variable name | Optional. The names of other variables you wish to create with the same data type. Each name should be separated with a comma. |
dataType | Data type name | The data type of the variable. |
InitialValue | Same type as dataType | Initial value for variableName. If the datatype is an object that requires instantiation via the New operator, you can do that instead of assigning an initial value. |
[= InitialValue]
Static arrayName(size [,sizeN]) as dataType
Part | Type | Description |
arrayName | Variable name | The name of the new array. |
size | Integer | The size (number of elements) for the array. |
sizeN | Integer | Optional. The size of the next dimension of the array if you are creating a multi-dimensional array. |
dataType | Data type name | The data type of the array. |
Notes
Variables created with the Static statement are local in scope. The value of a local variable can be accessed only from within the method. Unlike the Dim statement, variables created with the Static statement retain their values from one invocation of the method or function to the next. In other words, they are persistent local variables. In contrast, the values assigned to local variables created by the Dim statement are lost when the method goes out of scope.
The two syntaxes for Static are used for creating variables and arrays, respectively. In the first syntax, the Static statement is used to create new variables. A variable is an object stored in RAM that can hold a value. In the second syntax, the Static statement is used to create a new array of the size specified. An array is a variable that can contain multiple values that are all the same data type. Passing more than one size parameter creates a multi-dimensional array, with the number of dimensions equal to the number of size parameters passed.
You can optionally provide an initial value to a variable declared with a Static statement. If the variable is one of REALbasic's built-in data types ( String, Integer, Single, Double, Boolean, or Color) you can use the assignment statement to assign the initial value. Here are some examples:
Static b as Double =87.87
Static s as String="Howdy"
Static c as Color = &cFF4B51
The following statement initializes three variables to an initial value:
If the data type is an object, you can optionally instantiate the object with the New operator in the Static statement.This code declares and instantiates a Date object.
Otherwise, you need a second line of code to instantiate the variable, d, as in the following:
If the call to the object's constructor takes parameters, you can pass the parameters as well. Here is an example:
The Static statement can be placed anywhere in a method or function, including inside a conditional structure (an If or Case statement).
If you don't know the size of the array you need at the time you declare it, you can declare it as a null array, i.e., an array with no elements, and use the Redim command to resize it later. You do this by giving it an index of -1. This means "an array of no elements." For example, the statement:
creates the array aNames with no elements. If your program needs to load a list of names that the user enters, you can wait to size the array until you know how many names the user has entered. You can also accomplish this by leaving out the -1. The following statement is equivalent:
When you assign values to an array variable, such as with the Split function, you don't need to know the number of elements ahead of time.
You can also assign an array to another array. For example, the following is valid:
StaticString
myArray.Append "Anthony"
myArray.Append "Aardvark"
myArray.Append "Accountant"
newArray()=myArray //newArray set equal to myArray
Examples
This example uses the Static statement to create an Integer variable called "age" and assigns it the value 33.
This example uses the Static statement to create two String variables.
This example uses the Static statement to create an array called aNames with 11 elements (remember, arrays have a zero element).
This example uses the Static statement to create an array called aNames with one element.
See Also
Append, Insert, Redim, Remove, Sort, Sortwith methods; Ubound function; Collection, Dictionary classes; Dim statement.